package com.design.pattern.createmode.singleton;

/**
 * 创建型模式-单例模式-枚举方式
 * @author yll
 * @version 1.0.0
 * @date 2024/03/14 14：42
 * @desc 枚举方式进行实例化，是线程安全的，此种方式也是线程最安全的
 * */
public class SingletonExample_03_01 {

    private SingletonExample_03_01(){}

    private static SingletonExample_03_01 singleton = null;

    public static SingletonExample_03_01 getInstance(){
        if(singleton == null){
            singleton = SingletonExampleEnum.instance.getInstance();
        }
        return singleton;
    }

    /**
     * 枚举-枚举方式进行实例化，是线程安全的，此种方式也是线程最安全的
     * */
    private enum SingletonExampleEnum{
        instance;
        private SingletonExample_03_01 singleton;

        //JVM保证这个方法绝对只调用一次
        SingletonExampleEnum(){
            this.singleton = new SingletonExample_03_01();
        }
        public SingletonExample_03_01 getInstance(){
            return this.singleton;
        }
    }
}
